package com.fjrz.bbxwj.main.notice

import android.os.Bundle
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.LogUtils
import com.module.lemlin.custom.SpacingItemDecoration
import com.fjrz.bbxwj.R
import com.fjrz.bbxwj.entity.MessageComment
import com.fjrz.bbxwj.entity.MessageLike
import com.fjrz.bbxwj.entity.MessageReply
import com.fjrz.bbxwj.main.notice.adapter.MessageCommentAdapter
import com.fjrz.bbxwj.main.notice.adapter.MessageLikeAdapter
import com.fjrz.bbxwj.main.notice.adapter.MessageReplyAdapter
import com.fjrz.bbxwj.mode.vm.MessageViewModel
import com.module.lemlin.mode.ViewModeFragment
import com.module.lemlin.rxhttp.Status
import com.module.lemlin.rxhttp.msg
import kotlinx.android.synthetic.main.fragment_message_mutual.*

class MessageMutualFragment : ViewModeFragment<MessageViewModel>() {


    companion object {

        private const val EXTRA_NAME_TYPE = "EXTRA_NAME_TYPE"
        fun newInstance(type: Int): MessageMutualFragment {
            val bundle: Bundle = Bundle().apply {
                putInt(EXTRA_NAME_TYPE, type)
            }
            return MessageMutualFragment()
                .apply { arguments = bundle }
        }
    }

    private val mMessageLikeAdapter = MessageLikeAdapter()

    private val mMessageCommentAdapter = MessageCommentAdapter()

    private val mMessageReplyAdapter = MessageReplyAdapter()

    private val mType: Int
        get() = arguments?.getInt(EXTRA_NAME_TYPE) ?: 0

    override fun viewModel(): MessageViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.fragment_message_mutual

    override fun initView() {
        rvMessageMutualBody.apply {
            val decoration = SpacingItemDecoration(
                vGap = ConvertUtils.dp2px((15f)),
                hGap = ConvertUtils.dp2px((15f))
            )
            addItemDecoration(decoration)
        }.adapter = when (mType) {
            1 -> mMessageCommentAdapter
            2 -> mMessageReplyAdapter
            else -> mMessageLikeAdapter
        }


        //下拉刷新监听
        mSmartRefreshLayout.setOnRefreshListener {
            when (mType) {
                1 -> viewModel.messageComment()
                2 -> viewModel.messageReply()
                else -> viewModel.messageLike()
            }
        }
        //加载更多刷新监听
        mSmartRefreshLayout.setOnLoadMoreListener {
            when (mType) {
                1 -> viewModel.messageComment(isLoadMore = true)
                2 -> viewModel.messageReply(isLoadMore = true)
                else -> viewModel.messageLike(isLoadMore = true)
            }
        }
    }

    override fun initData() {
        when (mType) {
            1 -> viewModel.messageComment()
            2 -> viewModel.messageReply()
            else -> viewModel.messageLike()
        }
    }

    override fun initViewMode() {
        viewModel.messageLikeLiveData.observe(viewLifecycleOwner, Observer { response ->
            val status: Status = responseStatus1LiveData(response.status, response.throwable)
            if (status != Status.SUCCESS)
                return@Observer
            val isLoadMore: Boolean = response.httpData?.loadMore ?: false
            val messageLike: MutableList<MessageLike> =
                response.httpData?.data?.data ?: mutableListOf()
            if (messageLike.isNullOrEmpty()) {
                mMessageLikeAdapter.setEmptyView(createViewEmptyNull(hint = "暂无数据"))
            }
            if (isLoadMore) {
                mMessageLikeAdapter.addData(messageLike)
            } else {
                mMessageLikeAdapter.setNewInstance(messageLike)
            }
            if (messageLike.size < viewModel.limit) {
                mMessageLikeAdapter.setFooterView(createViewFooter())
                mSmartRefreshLayout.setEnableLoadMore(false)
            } else {
                mMessageLikeAdapter.removeAllFooterView()
                mSmartRefreshLayout.setEnableLoadMore(true)
            }
        })

        viewModel.messageCommentLiveData.observe(viewLifecycleOwner, Observer { response ->
            val status: Status = responseStatus2LiveData(response.status, response.throwable)
            if (status != Status.SUCCESS)
                return@Observer
            val isLoadMore: Boolean = response.httpData?.loadMore ?: false
            val messageComment: MutableList<MessageComment> =
                response.httpData?.data?.data ?: mutableListOf()
            if (messageComment.isNullOrEmpty()) {
                mMessageCommentAdapter.setEmptyView(createViewEmptyNull(hint = "暂无数据"))
            }
            if (isLoadMore) {
                mMessageCommentAdapter.addData(messageComment)
            } else {
                mMessageCommentAdapter.setNewInstance(messageComment)
            }
            if (messageComment.size < viewModel.limit) {
                mMessageCommentAdapter.setFooterView(createViewFooter())
                mSmartRefreshLayout.setEnableLoadMore(false)
            } else {
                mMessageCommentAdapter.removeAllFooterView()
                mSmartRefreshLayout.setEnableLoadMore(true)
            }
        })

        viewModel.messageReplyLiveData.observe(viewLifecycleOwner, Observer { response ->

            val status: Status = responseStatus3LiveData(response.status, response.throwable)
            if (status != Status.SUCCESS)
                return@Observer
            val isLoadMore: Boolean = response.httpData?.loadMore ?: false
            val messageReply: MutableList<MessageReply> =
                response.httpData?.data?.data ?: mutableListOf()
            if (messageReply.isNullOrEmpty()) {
                mMessageReplyAdapter.setEmptyView(createViewEmptyNull(hint = "暂无数据"))
            }
            if (isLoadMore) {
                mMessageReplyAdapter.addData(messageReply)
            } else {
                mMessageReplyAdapter.setNewInstance(messageReply)
            }
            if (messageReply.size < viewModel.limit) {
                mMessageReplyAdapter.setFooterView(createViewFooter())
                mSmartRefreshLayout.setEnableLoadMore(false)
            } else {
                mMessageReplyAdapter.removeAllFooterView()
                mSmartRefreshLayout.setEnableLoadMore(true)
            }
        })
    }


    private fun responseStatus1LiveData(status: Status, throwable: Throwable?): Status {
        if (status == Status.START) {
            mMessageLikeAdapter.setEmptyView(createViewEmptyLoad())
        } else if (status == Status.FAILURE) {
            mSmartRefreshLayout.finishLoadMore((false))
            mSmartRefreshLayout.finishRefresh((false))
            mMessageLikeAdapter.setEmptyView(createViewEmptyNull(hint = throwable?.msg ?: "未知错误"))
        } else {
            mSmartRefreshLayout.finishLoadMore((true))
            mSmartRefreshLayout.finishRefresh((true))
        }
        return status
    }


    private fun responseStatus2LiveData(status: Status, throwable: Throwable?): Status {
        if (status == Status.START) {
            mMessageCommentAdapter.setEmptyView(createViewEmptyLoad())
        } else if (status == Status.FAILURE) {
            mSmartRefreshLayout.finishLoadMore((false))
            mSmartRefreshLayout.finishRefresh((false))
            mMessageCommentAdapter.setEmptyView(
                createViewEmptyNull(
                    hint = throwable?.msg ?: "未知错误"
                )
            )
        } else {
            mSmartRefreshLayout.finishLoadMore((true))
            mSmartRefreshLayout.finishRefresh((true))
        }
        return status
    }


    private fun responseStatus3LiveData(status: Status, throwable: Throwable?): Status {
        if (status == Status.START) {
            mMessageReplyAdapter.setEmptyView(createViewEmptyLoad())
        } else if (status == Status.FAILURE) {
            mSmartRefreshLayout.finishLoadMore((false))
            mSmartRefreshLayout.finishRefresh((false))
            mMessageReplyAdapter.setEmptyView(createViewEmptyNull(hint = throwable?.msg ?: "未知错误"))
        } else {
            mSmartRefreshLayout.finishLoadMore((true))
            mSmartRefreshLayout.finishRefresh((true))
        }
        return status
    }

}